package com.integrated;

import java.awt.image.LookupOp;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.Scanner;

public class upGrade {
    public static void main(String[] args) {
        ArrayList<User> users = new ArrayList<>();
        while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择操作 ； 1.登录  2.注册  3.忘记密码");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose) {
                case "1":
                    login(users);
                    break;
                case "2":
                    register(users);
                    break;
                case "3":
                    System.out.println("忘记密码");
                    break;
                default:
                    System.out.println("选择错误，请重新选择!");
                    break;
            }
        }
    }

    public static void login(ArrayList<User> users) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String name = sc.next();
        System.out.println("请输入密码：");
        String password = sc.next();
        if (contains(users, name) >= 0) {
            String real_password = users.get(contains(users, name)).getUser_password();
            if (real_password.equals(password)) {
                System.out.println("登录成功！");
            } else System.out.println("登录失败，用户名或密码错误！");
        } else System.out.println("用户不存在！");
    }

    public static void register(ArrayList<User> users) {
        String password;
        User user = new User();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入用户名：");
            String name = sc.next();
            if (contains(users, name) >= 0) {
                System.out.println("用户名已存在，请重新输入用户名！");
            } else {
                user.setUser_name(name);
                break;
            }
        }
        while (true) {
            System.out.println("请输入3-15位密码：");
            password = sc.next();
            if (password.length() > 15 || password.length() < 3) {
                System.out.println("密码位数不对！");
            } else {
                System.out.println("请再次输入密码：");
                String password2 = sc.next();
                if (password2.equals(password)) {
                    user.setUser_password(password2);
                    break;
                } else System.out.println("密码不一致！");
            }
    }
        System.out.println("注册成功!");
        users.add(user);


}

    public static void forget(ArrayList<User> users) {

    }

    public static int contains(ArrayList<User> users, String name) {
        for (int i = 0; i < users.size(); i++) {
            if (users.get(i).getUser_name() == name) {
                return -1;
            } else return i;
        }
        return -1;
    }
}
